package com.xindaoapp.framework.api;

import android.content.Context;
import android.text.TextUtils;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.xindaoapp.framework.R;
import com.xindaoapp.framework.application.XindaoApplication;
import com.xindaoapp.framework.utils.CommonUtil;
import com.xindaoapp.framework.utils.IRequest;

public class NewApiImpl implements NewApi {
	private static Context context;
	public static NewApiImpl newApi;

	public static NewApiImpl getInstance() {
		context = XindaoApplication.get().getContext();
		newApi = new NewApiImpl();
		return newApi;
	}

	private NewApiImpl() {
		super();
	}

	@Override
	public void getBanner(final IRequest<String> iRequest) {
		if (CommonUtil.checkNetState(context)) {
			AddClient.post(NewApi.SHOP_BANNER, null, new AsyncHttpResponseHandler() {
				@Override
				public void onSuccess(int statusCode, String content) {
					iRequest.request(content);
					super.onSuccess(statusCode, content);
				}

				@Override
				public void onFailure(Throwable error, String content) {
					iRequest.request("error");
					super.onFailure(error, content);
				}
			});
		} else {
			Toast.makeText(context, R.string.check_netstatus, Toast.LENGTH_SHORT).show();
		}

	}

	@Override
	public void getCategoryOne(final IRequest<String> iRequest) {
		if (CommonUtil.checkNetState(context)) {
			AddClient.get(NewApi.SHOP_CATEGORY_ONE, null, new AsyncHttpResponseHandler() {
				@Override
				public void onSuccess(int statusCode, String content) {
					iRequest.request(content);
					super.onSuccess(statusCode, content);
				}

				@Override
				public void onFailure(Throwable error, String content) {
					iRequest.request("error");
					super.onFailure(error, content);
				}
			});
		} else {
			Toast.makeText(context, R.string.check_netstatus, Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void getCategoryTwo(String parentID, final IRequest<String> iRequest) {
		if (CommonUtil.checkNetState(context)) {
			RequestParams params = new RequestParams();
			params.put("parentID", parentID);
			AddClient.get(NewApi.SHOP_CATEGORY_ONE, params, new AsyncHttpResponseHandler() {
				@Override
				public void onSuccess(int statusCode, String content) {
					iRequest.request(content);
					super.onSuccess(statusCode, content);
				}

				@Override
				public void onFailure(Throwable error, String content) {
					iRequest.request("error");
					super.onFailure(error, content);
				}
			});
		} else {
			Toast.makeText(context, R.string.check_netstatus, Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void getNewGoods(int page, int pageSize, final IRequest<String> iRequest) {
		if (CommonUtil.checkNetState(context)) {
			RequestParams params = new RequestParams();
			params.put("page", String.valueOf(page));
			params.put("pageSize", String.valueOf(pageSize));
			AddClient.post(NewApi.SHOP_NEW_GOODS, params, new AsyncHttpResponseHandler() {
				@Override
				public void onSuccess(int statusCode, String content) {
					iRequest.request(content);
					super.onSuccess(statusCode, content);
				}

				@Override
				public void onFailure(Throwable error, String content) {
					iRequest.request("error");
					super.onFailure(error, content);
				}
			});
		} else {
			Toast.makeText(context, R.string.check_netstatus, Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void getTuan(int page, int pageSize, final IRequest<String> iRequest) {
		if (CommonUtil.checkNetState(context)) {
			RequestParams params = new RequestParams();
			params.put("page", String.valueOf(page));
			params.put("pageSize", String.valueOf(pageSize));
			AddClient.post(NewApi.SHUP_TUAN, params, new AsyncHttpResponseHandler() {
				@Override
				public void onSuccess(int statusCode, String content) {
					iRequest.request(content);
					super.onSuccess(statusCode, content);
				}

				@Override
				public void onFailure(Throwable error, String content) {
					iRequest.request("error");
					super.onFailure(error, content);
				}
			});
		} else {
			Toast.makeText(context, R.string.check_netstatus, Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void getCats(int page, int pageSize, String catID, String catName, final IRequest<String> iRequest) {
		RequestParams params=new RequestParams();
		params.put("page", String.valueOf(page));
		if(!TextUtils.isEmpty(catID)){
			params.put("catID", catID);
		}else{
			if(!TextUtils.isEmpty(catName)){
				params.put("catName", catName);
			}
		}
		params.put("pageSize", pageSize+"");
		AddClient.post(NewApi.GOODSLIST, params, new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, String content) {
				iRequest.request(content);
				super.onSuccess(statusCode, content);
			}
			@Override
			public void onFailure(Throwable error, String content) {
				iRequest.request("error");
				super.onFailure(error, content);
			}
		});
	}
}
